<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录状态检查</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            background: white;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        .status-section {
            margin-bottom: 30px;
            padding: 20px;
            border: 1px solid #ddd;
            border-radius: 5px;
        }
        .status-section h2 {
            margin-top: 0;
            color: #333;
        }
        .status-item {
            margin: 10px 0;
            padding: 10px;
            background: #f8f9fa;
            border-radius: 3px;
        }
        .status-success {
            border-left: 4px solid #28a745;
            background: #d4edda;
        }
        .status-error {
            border-left: 4px solid #dc3545;
            background: #f8d7da;
        }
        .status-warning {
            border-left: 4px solid #ffc107;
            background: #fff3cd;
        }
        .btn {
            background: #007bff;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 3px;
            cursor: pointer;
            margin-right: 10px;
            margin-bottom: 10px;
        }
        .btn:hover {
            background: #0056b3;
        }
        .btn-success {
            background: #28a745;
        }
        .btn-success:hover {
            background: #218838;
        }
        .btn-danger {
            background: #dc3545;
        }
        .btn-danger:hover {
            background: #c82333;
        }
        .code-block {
            background: #f8f9fa;
            border: 1px solid #e9ecef;
            border-radius: 3px;
            padding: 15px;
            font-family: monospace;
            white-space: pre-wrap;
            margin: 10px 0;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>登录状态检查工具</h1>
        
        <div class="status-section">
            <h2>当前登录状态</h2>
            <div id="loginStatus"></div>
        </div>
        
        <div class="status-section">
            <h2>操作</h2>
            <button onclick="checkLoginStatus()" class="btn">检查登录状态</button>
            <button onclick="simulateUserLogin()" class="btn btn-success">模拟用户登录</button>
            <button onclick="clearLoginData()" class="btn btn-danger">清除登录数据</button>
            <button onclick="goToLogin()" class="btn">前往登录页面</button>
        </div>
        
        <div class="status-section">
            <h2>诊断信息</h2>
            <div id="diagnosticInfo"></div>
        </div>
        
        <div class="status-section">
            <h2>常见问题解决</h2>
            <div class="status-item status-warning">
                <strong>问题1: 页面闪退</strong><br>
                原因: 用户未登录或登录信息无效<br>
                解决: 点击"模拟用户登录"或前往登录页面重新登录
            </div>
            <div class="status-item status-warning">
                <strong>问题2: API请求失败</strong><br>
                原因: 后端服务未启动或数据库连接失败<br>
                解决: 确保应用已启动，数据库连接正常
            </div>
            <div class="status-item status-warning">
                <strong>问题3: 用户ID为undefined</strong><br>
                原因: 登录时用户信息存储不完整<br>
                解决: 清除登录数据，重新登录
            </div>
        </div>
    </div>

    <script>
        function checkLoginStatus() {
            const userStr = localStorage.getItem('currentUser');
            const userType = localStorage.getItem('userType');
            const statusDiv = document.getElementById('loginStatus');
            
            let statusHTML = '';
            
            if (!userStr) {
                statusHTML += '<div class="status-item status-error">❌ 未登录 - 没有找到用户信息</div>';
            } else {
                try {
                    const user = JSON.parse(userStr);
                    if (!user || !user.userId) {
                        statusHTML += '<div class="status-item status-error">❌ 登录信息无效 - 用户ID缺失</div>';
                    } else {
                        statusHTML += '<div class="status-item status-success">✅ 已登录</div>';
                        statusHTML += `<div class="status-item">用户ID: ${user.userId}</div>`;
                        statusHTML += `<div class="status-item">用户名: ${user.username || '未知'}</div>`;
                    }
                } catch (error) {
                    statusHTML += `<div class="status-item status-error">❌ 登录信息解析失败: ${error.message}</div>`;
                }
            }
            
            if (!userType) {
                statusHTML += '<div class="status-item status-error">❌ 用户类型未设置</div>';
            } else {
                statusHTML += `<div class="status-item">用户类型: ${userType}</div>`;
            }
            
            statusDiv.innerHTML = statusHTML;
            
            // 更新诊断信息
            updateDiagnosticInfo();
        }
        
        function simulateUserLogin() {
            const user = {
                userId: 1,
                username: 'user1',
                passwordHash: 'test'
            };
            
            localStorage.setItem('currentUser', JSON.stringify(user));
            localStorage.setItem('userType', 'user');
            
            alert('模拟登录成功！现在可以访问用户页面了。');
            checkLoginStatus();
        }
        
        function clearLoginData() {
            localStorage.removeItem('currentUser');
            localStorage.removeItem('userType');
            alert('登录数据已清除！');
            checkLoginStatus();
        }
        
        function goToLogin() {
            window.location.href = 'login.html';
        }
        
        function updateDiagnosticInfo() {
            const userStr = localStorage.getItem('currentUser');
            const userType = localStorage.getItem('userType');
            const diagnosticDiv = document.getElementById('diagnosticInfo');
            
            let info = 'LocalStorage 内容:\n';
            info += `currentUser: ${userStr || 'null'}\n`;
            info += `userType: ${userType || 'null'}\n\n`;
            
            if (userStr) {
                try {
                    const user = JSON.parse(userStr);
                    info += '解析后的用户信息:\n';
                    info += JSON.stringify(user, null, 2);
                } catch (error) {
                    info += `解析错误: ${error.message}`;
                }
            }
            
            diagnosticDiv.innerHTML = `<div class="code-block">${info}</div>`;
        }
        
        // 页面加载时自动检查
        document.addEventListener('DOMContentLoaded', function() {
            checkLoginStatus();
        });
    </script>
</body>
</html> 